package com.elise;

import java.util.Calendar;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.Spinner;
import android.widget.TextView;

public class HomeSectionFragment extends Fragment implements OnClickListener, OnDismissListener {

	//Represents whether the counter is activated or not.
	private boolean c_activated; 
	
	private EditText name;
	private Spinner sign;
	private Spinner sign2;
	private TextView time_in;
	private TextView time_out;
	private Chronometer chron;
	private Button start;
	
	private Calendar cal;
	
	private Etime etime;
	private EtimePopup popup;
	
	@Override
	public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		
		View view = inflater.inflate(R.layout.fragment_home, container, false);
		view.getBackground().setAlpha(100);
		
		name = (EditText) view.findViewById(R.id.fragment_home_name);
		sign = (Spinner) view.findViewById(R.id.fragment_home_sign);
		sign2 = (Spinner) view.findViewById(R.id.fragment_home_sign2);
		time_in = (TextView) view.findViewById(R.id.time_in);
		time_out = (TextView) view.findViewById(R.id.time_out);	
		chron = (Chronometer) view.findViewById(R.id.chronometer);
		start = (Button) view.findViewById(R.id.btn_home_start);
		
		Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "fonts/clock_font.ttf");
		chron.setTypeface(tf);
		time_in.setTypeface(tf);
		time_out.setTypeface(tf);
		
		time_in.setText("--:--:--");
		time_out.setText("--:--:--");
		time_in.setTextColor(getResources().getColor(android.R.color.holo_blue_dark));
		time_out.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
		
		//SPINNER DEV
		ArrayAdapter<String> adapt = new ArraySignsAdapter(getActivity(), R.layout.sign_spinner_row, Etime.signs);
		sign.setAdapter(adapt);
		sign2.setAdapter(adapt);
						
		//POPUP DEVELOPMENT
		etime = new Etime();
		popup = new EtimePopup(getActivity(), container);
		popup.setOnDismissListener(this);
		
		//SETTING LISTENERS
		start.setOnClickListener(this);
		
		setEditablesVisibility(View.INVISIBLE);
		
		return view;
	}
	
	private void showPopup() {
		start.setVisibility(View.INVISIBLE);
		
		//Updating data to etime
		etime.setName(name.getText().toString().trim());
		etime.setSign(sign.getSelectedItemPosition());
		etime.setSign2(sign2.getSelectedItemPosition());
		
		popup.show(etime);
	}
	
	private void setEditablesVisibility(int v) {
		name.setVisibility(v);
		sign.setVisibility(v);
		sign2.setVisibility(v);
	}
	
	@Override
	public void onClick(View v) {
		
		//If the chronometer is activated
		if (!c_activated) {
			chron.setBase(SystemClock.elapsedRealtime());
			chron.start();
			
			cal = Calendar.getInstance();
			etime.setTime_in(cal.getTime());
			time_in.setText(Etime.timeFormat.format(etime.getTime_in()));
			
			c_activated = true;
			setEditablesVisibility(View.VISIBLE);
			
			//Color Button change
			start.setBackgroundResource(R.drawable.rounded_red_button);
			start.setText(R.string.stop);
		}
		else {
			
			chron.stop();
			cal = Calendar.getInstance();
			etime.setTime_out(cal.getTime());
			time_out.setText(Etime.timeFormat.format(etime.getTime_out()));
			
			c_activated = false;
			setEditablesVisibility(View.INVISIBLE);
			
			showPopup();
			
			//Color Button change
			start.setBackgroundResource(R.drawable.rounded_blue_button);
			start.setText(R.string.start);
		}
	}
	
	//When popup hides restore the initial visibility status
	@Override
	public void onDismiss() {
		name.setText("");
		sign.setSelection(0);
		sign2.setSelection(0);
		time_in.setText("--:--:--");
		time_out.setText("--:--:--");
		chron.setBase(SystemClock.elapsedRealtime());
		
		start.setVisibility(View.VISIBLE);
	}
	
	private class ArraySignsAdapter extends ArrayAdapter<String> {
		
		public ArraySignsAdapter(Context context, int textViewResourceId, String[] signs) {
			super(context, textViewResourceId, signs);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			return getCustomView(position, convertView, parent);
		}
		
		@Override
		public View getDropDownView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			return getCustomView(position, convertView, parent);
		}
		
		private View getCustomView(int position, View convertView, ViewGroup parent) {
			
			View row = getActivity().getLayoutInflater().inflate(R.layout.sign_spinner_row, parent, false);
			ImageView img = (ImageView) row.findViewById(R.id.spinner_row_img);
			TextView s = (TextView) row.findViewById(R.id.spinner_row_sign);
			
			img.setImageResource(Etime.signsImg[position]);
			s.setText(Etime.signs[position]);
			
			return row;
		}
	}
}
